/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simplemetrosystem.view.bar;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToolBar;
import simplemetrosystem.control.Calculation.ModelManager;
import simplemetrosystem.control.EditCommandManager;
import simplemetrosystem.model.charlanguage.NameMap;
import simplemetrosystem.view.action.NewFileActionListener;
import simplemetrosystem.view.action.OpenFileActionListener;
import simplemetrosystem.view.action.SaveAsActionListener;
import simplemetrosystem.view.action.SaveFileActionListener;
import simplemetrosystem.view.action.UndoActionListener;
import simplemetrosystem.view.MetroFrame;
import simplemetrosystem.view.NewMapSubject;
import simplemetrosystem.view.action.CalculActionListener;
import simplemetrosystem.view.action.RedoActionListener;

/**
 *
 * @author Eric Qians
 */
public class SystemToolBar extends JToolBar implements Observer {

    /**
     * 
     * @param frame
     */
    public SystemToolBar(MetroFrame frame) {
        super();
        this.frame = frame;
        nameMap = NameMap.getInstance();
        name = nameMap.getName("Edit");
        this.setName(name);
        this.setEnabled(true);
        this.setFloatable(false);
        this.setOrientation(JToolBar.HORIZONTAL);
        this.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));

        EditCommandManager.getInstance().addObserver(this);

        addItem();
    }

    /**
     * 
     */
    private void addItem() {
        ecGroup = new ButtonGroup();
        this.add(getNewButton());
        this.add(getOpenButton());
        this.add(getSaveButton());
        this.add(getSaveAsButton());
        this.add(getUndoButton());
        this.add(getRedoButton());
        this.add(getEditButton());
        this.add(getCalButton());
    }

    /**
     * 
     * @return
     */
    public JButton getSaveButton() {
        if (saveButton == null) {
            saveButton = new JButton();
            Icon icon = new ImageIcon("icon/save.gif");
            saveButton.setIcon(icon);
            //saveButton.setText(nameMap.getName("Save"));
            saveButton.setToolTipText(nameMap.getName("Save"));
            saveButton.addActionListener(new SaveFileActionListener(frame));
        }
        return saveButton;
    }

    /**
     * 
     * @return
     */
    public JButton getSaveAsButton() {
        if (saveAsButton == null) {
            saveAsButton = new JButton();
            Icon icon = new ImageIcon("icon/saveas.gif");
            saveAsButton.setIcon(icon);
            //saveAsButton.setText(nameMap.getName("Save As"));
            saveAsButton.setToolTipText(nameMap.getName("Save As"));
            saveAsButton.addActionListener(new SaveAsActionListener(frame));
        }
        return saveAsButton;
    }

    /**
     * 
     * @return
     */
    public JButton getNewButton() {
        if (newButton == null) {
            newButton = new JButton();
            //newButton.setText(nameMap.getName("New"));
            Icon icon = new ImageIcon("icon/new.gif");
            newButton.setIcon(icon);
            newButton.setToolTipText(nameMap.getName("New"));
            newButton.addActionListener(new NewFileActionListener(frame));
        }
        return newButton;
    }

    /**
     * 
     * @return
     */
    public JButton getOpenButton() {
        if (openButton == null) {
            openButton = new JButton();
            Icon icon = new ImageIcon("icon/open.gif");
            openButton.setIcon(icon);
            //openButton.setText(nameMap.getName("Open"));
            openButton.setToolTipText(nameMap.getName("Open"));
            openButton.addActionListener(new OpenFileActionListener(frame));
        }
        return openButton;
    }

    /**
     * 
     * @return
     */
    public JButton getUndoButton() {
        if (undoButton == null) {
            undoButton = new JButton();
            Icon icon = new ImageIcon("icon/undo.gif");
            undoButton.setIcon(icon);
            //undoButton.setText(nameMap.getName("Undo"));
            undoButton.setToolTipText(nameMap.getName("Undo"));
            undoButton.setEnabled(EditCommandManager.getInstance().undoEnabled());
            undoButton.addActionListener(new UndoActionListener(frame));
        }
        return undoButton;
    }

    /**
     * 
     * @return
     */
    public JButton getRedoButton() {
        if (redoButton == null) {
            redoButton = new JButton();
            Icon icon = new ImageIcon("icon/redo.gif");
            redoButton.setIcon(icon);
            //redoButton.setText(nameMap.getName("Redo"));
            redoButton.setToolTipText(nameMap.getName("Redo"));
            redoButton.setEnabled(EditCommandManager.getInstance().redoEnabled());
            redoButton.addActionListener(new RedoActionListener(frame));
        }
        return redoButton;
    }

    public JButton getCalButton() {
        if (calButton == null) {
            calButton = new JButton();
            Icon icon = new ImageIcon("icon/calmodel.gif");
            calButton.setIcon(icon);
            //calButton.setText(nameMap.getName("Calculation Model"));
            calButton.setToolTipText(nameMap.getName("Calculation Model"));
            ecGroup.add(calButton);
            calButton.addActionListener(new CalculActionListener(frame));
        }
        return calButton;
    }

    public JButton getEditButton() {
        if (editButton == null) {
            editButton = new JButton();
            Icon icon = new ImageIcon("icon/editmodel.gif");
            editButton.setIcon(icon);
            //editButton.setText(nameMap.getName("Edit Model"));
            editButton.setToolTipText(nameMap.getName("Edit Model"));
            editButton.setEnabled(false);
            ecGroup.add(editButton);
            editButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    ModelManager.getInstance().setModel(false);
                }
            });

        }
        return editButton;
    }

    public void update(Observable o, Object arg) {
        if (o instanceof NewMapSubject) {
            EditCommandManager manager = EditCommandManager.getInstance();
            manager.addObserver(this);
            getUndoButton().setEnabled(manager.undoEnabled());
            getRedoButton().setEnabled(manager.redoEnabled());
        } else if (o instanceof EditCommandManager) {
            EditCommandManager manager = (EditCommandManager) o;
            getUndoButton().setEnabled(manager.undoEnabled());
            getRedoButton().setEnabled(manager.redoEnabled());
        }
    }

    public void setModel(boolean model) {
        if (this.model != model) {
            this.model = model;
            if (model) {
                getCalButton().setEnabled(false);
                getEditButton().setEnabled(true);
                getUndoButton().setEnabled(false);
                getRedoButton().setEnabled(false);
            } else {
                EditCommandManager manager = EditCommandManager.getInstance();
                getCalButton().setEnabled(true);
                getEditButton().setEnabled(false);
                getUndoButton().setEnabled(manager.undoEnabled());
                getRedoButton().setEnabled(manager.redoEnabled());
            }
        }
    }
    private String name;
    private NameMap nameMap;
    private MetroFrame frame;
    private JButton newButton;
    private JButton openButton;
    private JButton saveButton;
    private JButton saveAsButton;
    private JButton undoButton;
    private JButton redoButton;
    private JButton editButton;
    private JButton calButton;
    private ButtonGroup ecGroup;
    private boolean model;
}
